<?php

declare(strict_types=1);

namespace App\Model\PssModel;

use App\Model\BaseModel\BasePssModel;
use Hyperf\DbConnection\Db;
class OrderRefundGoods extends BasePssModel
{
    public $table = 'order_refund_goods';
    
    /**
     * 统计已售后的商品数据
     * @param string $order_id
     * @param array $skucodes
     */
    public function getByGoodsRefundItems(string $order_id,array $skucodes)
    {
        return self::query()
        ->select([
            'skucode',
            'spec_num',
            Db::raw("sum(refund_num) AS total_refund_goods_count"),
            Db::raw("sum(refund_money) AS total_refund_money"),
            Db::raw("sum(refund_delivery_money) AS total_refund_delivery_money"),
            Db::raw("sum(refund_goods_money) AS total_refund_goods_money"),
            Db::raw("sum(refund_discount_money) AS total_refund_discount_money"),
            Db::raw("sum(refund_discount_points_money) AS total_refund_discount_point_money"),
            Db::raw("sum(refund_discount_envelope_money) AS total_refund_discount_red_envelope_money"),
            Db::raw("sum(refund_discount_activity_money) AS total_refund_discount_activity_money"),
            Db::raw("sum(refund_discount_coupon_money) AS total_refund_discount_coupon_money"),
            Db::raw("sum(refund_discount_payway_money) AS total_refund_discount_payway_money"),
            Db::raw("sum(refund_discount_precard_money) AS total_refund_discount_precard_money"),
            Db::raw("sum(refund_discount_points) AS total_refund_discount_points"),
            Db::raw("sum(refund_discount_deposit_goods_money) AS total_refund_discount_deposit_goods_money"),
            Db::raw("sum(refund_discount_deposit_delivery_money) AS total_refund_discount_deposit_delivery_money"),
        ])
        ->where('order_id',$order_id)
        ->whereIn('skucode',$skucodes)
        ->where('refund_useable',1)
        ->groupBy('skucode')
        ->groupBy('spec_num')
        ->get()
        ->toArray();
    }

    /**
     * 统计已售后的订单商品数据
     * @param string $order_id 订单id
     */
    public function getRefundGoodsItemsByOrderId(string $order_id)
    {
        return self::query()
        ->select([
            'skucode',
            'spec_num',
            Db::raw("sum(refund_num) AS total_refund_goods_count"),
            Db::raw("sum(refund_money) AS total_refund_money"),
            Db::raw("sum(refund_delivery_money) AS total_refund_delivery_money"),
            Db::raw("sum(refund_goods_money) AS total_refund_goods_money"),
            Db::raw("sum(refund_discount_money) AS total_refund_discount_money"),
            Db::raw("sum(refund_discount_points_money) AS total_refund_discount_point_money"),
            Db::raw("sum(refund_discount_envelope_money) AS total_refund_discount_red_envelope_money"),
            Db::raw("sum(refund_discount_activity_money) AS total_refund_discount_activity_money"),
            Db::raw("sum(refund_discount_coupon_money) AS total_refund_discount_coupon_money"),
            Db::raw("sum(refund_discount_payway_money) AS total_refund_discount_payway_money"),
            Db::raw("sum(refund_discount_precard_money) AS total_refund_discount_precard_money"),
            Db::raw("sum(refund_discount_points) AS total_refund_discount_points"),
            Db::raw("sum(refund_discount_deposit_goods_money) AS total_refund_discount_deposit_goods_money"),
            Db::raw("sum(refund_discount_deposit_delivery_money) AS total_refund_discount_deposit_delivery_money"),
        ])
        ->where('order_id',$order_id)
        ->where('refund_useable',1)
        ->groupBy('skucode')
        ->groupBy('spec_num')
        ->get()
        ->toArray();
    }

    /**
     * 统计订单是否已发生售后
     * @param string $order_id
     * @param array $skucodes
     */
    public function getOrderIsRefund(string $order_id)
    {
        $row = self::query()
        ->select([
            'id'
        ])
        ->where('order_id',$order_id)
        ->where('refund_useable',1)
        ->first();
        return $row ? true : false;
    }
    
    /**
     * 获取售后商品
     * @param string $refund_order_id
     */
    public function getRefundGoods(string $refund_order_id)
    {
        return self::query()->where('refund_order_id',$refund_order_id)->get()->toArray();
    }
}
